﻿Imports System.Xml

Public Class Streaming
    Dim strApiKey As String
    Dim strSessionID As String
    Dim arrChannels(1024) As channel
    Public lastChannel As Integer = 0

    Public Const TYPE_3GP As String = "3gp"
    Public Const TYPE_RM As String = "rm"
    Public Const TYPE_ASX As String = "asx"
    Public Const TYPE_FLV As String = "flv"


    Structure channel

        Dim orbTitle As String
        Dim orbMediumID As String
        Dim isFavorite As Boolean


        Property listItem() As ListItem
            Get
                Dim strFav As String
                If isFavorite Then
                    strFav = " (FAV)"
                Else
                    strFav = ""
                End If

                Return New ListItem(orbMediumID, orbTitle & strFav)
            End Get
            Set(ByVal value As ListItem)

            End Set
        End Property
    End Structure

    Sub New(ByVal apikey As String, ByVal sessionID As String)
        strApiKey = apikey
        strSessionID = sessionID
    End Sub

    Function GetChannelByName(ByVal name As String) As ListItem
        Dim intTeller
        Dim title As String
        title = Replace(name, " (FAV)", "")
        title = Trim(title)

        For intTeller = 1 To 1024
            Debug.Print(arrChannels(intTeller).orbTitle)
            If arrChannels(intTeller).orbTitle = title Then
                Return arrChannels(intTeller).listItem
            End If
        Next
        Return Nothing
    End Function

    Sub RetrieveChannels()

        Call EmptyArray()

        Dim intCounter As Integer = 1

        Dim oComm As New Communication
        Dim oReader As XmlNodeReader = oComm.ConvertStringToXmlReader(oComm.getResponseFromOrb(Communication.APITvGetListings, "sid=" & strSessionID))
        While oReader.Read
            If oReader.NodeType = XmlNodeType.Element Then
                If oReader.Name = "channel" Then
                    arrChannels(intCounter) = New channel
                    arrChannels(intCounter).orbMediumID = oReader.GetAttribute("orbMediumId")
                    arrChannels(intCounter).orbTitle = oReader.GetAttribute("title")
                    If oReader.GetAttribute("rating") > 0 Then
                        arrChannels(intCounter).isFavorite = True
                    End If
                    intCounter = intCounter + 1
                    Debug.Print(intCounter)
                End If
            End If
        End While
        lastChannel = intCounter
    End Sub

    Function GetChannel(ByVal num As Integer) As ListItem
        Return arrChannels(num).listItem
    End Function

    Function GetChannel(ByVal mediumid As String) As ListItem
        Dim intTeller
        For intTeller = 1 To 1024
            If arrChannels(intTeller).orbMediumID = mediumid Then
                Return arrChannels(intTeller).listItem
            End If
        Next
        Return Nothing
    End Function

    Function GetChannelByTitle(ByVal title As String) As ListItem
        Dim intTeller
        For intTeller = 1 To 1024
            If arrChannels(intTeller).orbTitle = title Then
                Return arrChannels(intTeller).listItem
            End If
        Next
        Return Nothing
    End Function

    Sub emptyArray()
        
        lastChannel = 0
    End Sub

    Function GetStreamingUri(ByVal type As String, ByVal orbMediumID As String) As String
        If orbMediumID = "" Then Throw New OrbInternalError
        Dim oParamBuilder As New ParamBuilder
        Dim streamUri As String = ""
        oParamBuilder.addParam("sid", strSessionID)
        oParamBuilder.addParam("streamFormat", type)
        oParamBuilder.addParam("mediumId", orbMediumID)
        Dim oComm As New Communication
        Dim oReader As XmlNodeReader = oComm.ConvertStringToXmlReader(oComm.getResponseFromOrb(Communication.APIStream, oParamBuilder.GetParamList))

        While oReader.Read
            If oReader.NodeType = XmlNodeType.Element Then
                If oReader.Name = "item" Then
                    streamUri = oReader.GetAttribute("url")
                End If
            End If
        End While
        Return streamUri
    End Function

    Function GetStreamingUri(ByVal type As String, ByVal orbMediumID As String, ByVal width As Integer, ByVal height As Integer) As String
        If orbMediumID = "" Then Throw New OrbInternalError
        Dim oParamBuilder As New ParamBuilder
        Dim streamUri As String = ""
        oParamBuilder.addParam("sid", strSessionID)
        oParamBuilder.addParam("streamFormat", type)
        oParamBuilder.addParam("mediumId", orbMediumID)
        oParamBuilder.addParam("height", height)
        oParamBuilder.addParam("width", width)
        Dim oComm As New Communication
        Dim oReader As XmlNodeReader = oComm.ConvertStringToXmlReader(oComm.getResponseFromOrb(Communication.APIStream, oParamBuilder.GetParamList))

        While oReader.Read
            If oReader.NodeType = XmlNodeType.Element Then
                If oReader.Name = "item" Then
                    streamUri = oReader.GetAttribute("url")
                End If
            End If
        End While

        Return streamUri
    End Function


End Class

Public Class ListItem

    Private _value As Object
    Private _text As String

    Sub New(ByVal value As Object, ByVal text As String)
        _value = value
        _text = text
    End Sub
    Public Property Value() As Object
        Get
            Return _value
        End Get
        Set(ByVal value As Object)
            _value = value
        End Set
    End Property

    Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return _text
    End Function

End Class